const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 开发目录路径
const src = path.resolve(__dirname, 'src');
// 生产目路径
const publish = path.resolve(__dirname, 'publish');
// webpack 配置对象
const config = {
  context: src,
  entry: src,
  output: {
    path: publish,
    publicPath: '/',
    filename: '[hash].js'
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: publish,
    compress: true,
    port: 9000,
    // host: "0.0.0.0"
  },
  module: {
    rules: [{
      test: /\.style$/,
      use: ['style-loader']
    }, {
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [
          'css-loader',
          'autoprefixer-loader'
        ]
      })
    }, {
      test: /\.scss$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [
          'css-loader',
          'autoprefixer-loader',
          'sass-loader'
        ]
      })
    }, {
      test: /\.js[x]?$/,
      exclude: /(node_modules)/,
      loader: 'babel-loader',
      query: {
        presets: [ "es2015", "stage-2", "react" ]
      }
    }]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: 'vendors.[hash].js',
      children: true,
      minChunks: 2
    }),
    new webpack.optimize.DedupePlugin(),// 相当于命令参数 --optimize-dedupe 消除冗余的或重复的代码
    new webpack.optimize.AggressiveMergingPlugin(), //用来优化生成的代码 chunk,合并相同的代码
    // new webpack.NoErrorsPlugin(), //用来保证编译过程不出错
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new ExtractTextPlugin('[hash].css'),
    new HtmlWebpackPlugin({
      title: 'nihao',
      template: [src, 'template', 'src', 'index.ejs'].join('/'),
      inject: true,
      hash: true
    }),
  ]
};

module.exports = config;
